package com.example.newnoke.ui;

import android.databinding.DataBindingUtil;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.example.newnoke.R;
import com.example.newnoke.data.BaseDatas;
import com.example.newnoke.data.FTData;
import com.example.newnoke.databinding.ActivityFtBinding;
import com.example.newnoke.mvp.presenter.FtPresenter;
import com.example.newnoke.mvp.view.IFtV;
import com.example.newnoke.utils.API;
import com.geya.jbase.baseactivity.BaseDetailsActivity;
import com.geya.jbase.constant.RequestType;
import com.geya.jbase.uiview.ToastUtil;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FTActivity extends BaseDetailsActivity<FtPresenter> implements IFtV {

    private ActivityFtBinding mBinding;

    private int num = 1;
    private String name;
    private String prices;
    private double price;

    @Override
    public FtPresenter newPresenter() {
        return new FtPresenter(this);
    }

    @Override
    public void init() {
        mTitleButton.setTitles("复投");
        HashMap<String, String> map = new HashMap<>();
        map.put("type", "2");
        mPresenter.accessServers(RequestType.OKGO_POST, API.REBUY, FTData.class, map);

        mBinding.pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                HashMap<String, String> map = new HashMap<>();
                map.put("type", "2");
                map.put("goods_name", name);
                map.put("pay_bonus", mBinding.etPrice1.getText().toString().isEmpty() ? "0" : mBinding.etPrice1.getText().toString());
                map.put("pay_share", mBinding.etPrice2.getText().toString().isEmpty() ? "0" : mBinding.etPrice2.getText().toString());
                map.put("pay_team", mBinding.etPrice3.getText().toString().isEmpty() ? "0" : mBinding.etPrice3.getText().toString());
                map.put("price", prices);
                map.put("total_num", mBinding.num.getText().toString().isEmpty() ? "1" : mBinding.num.getText().toString());

                mPresenter.accessServers(RequestType.OKGO_POST, API.ORDER_REBUY, BaseDatas.class, map);
            }
        });


    }

    public void initClick() {
        price = Double.parseDouble(prices);
        num = Integer.parseInt(mBinding.num.getText().toString());
        mBinding.jia.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBinding.num.getText().toString().equals("")) {
                    mBinding.num.setText("0");
                }
                num = Integer.parseInt(mBinding.num.getText().toString());
                num++;
                if (num <= 0) {
                    num = 1;
                }
                mBinding.num.setText(num + "".trim());
                mBinding.price.setText(price * num + "");
            }
        });
        mBinding.jian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mBinding.num.getText().toString().equals("")) {
                    mBinding.num.setText("0");
                }
                num = Integer.parseInt(mBinding.num.getText().toString());
                num--;
                if (num <= 0) {
                    num = 1;
                }
                mBinding.num.setText(num + "".trim());
                mBinding.price.setText(price + "");
                mBinding.price.setText(price * num + "");
            }
        });


        mBinding.num.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String money = mBinding.num.getText().toString();
                Pattern p = Pattern.compile("[0-9]*");
                Matcher m = p.matcher(money);
                if (!mBinding.num.getText().toString().isEmpty()) {
                    num = Integer.parseInt(mBinding.num.getText().toString());
                    mBinding.price.setText(price * num + "");

                } else {
//                    ToastUtil.toastCenter(BuyCuurseActivity.this, "请输入正确的缴费金额");
//                    mNum.setText("1");
                    mBinding.price.setText("0");
                }

            }
        });
    }

    @Override
    public void setContentView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_ft);
    }


    @Override
    public void setData(FTData.DataBean data) {
        name = data.getGoods().getTitle();
        prices = data.getGoods().getPrice();
        initClick();
        mTitleButton.setTitles(data.getGoods().getTitle());
        mBinding.price.setText(data.getGoods().getPrice());
        mBinding.etPrice1.setHint("当前余额" + data.getUser().getBonus_amount() + "（点击输入）");
        mBinding.etPrice2.setHint("当前余额" + data.getUser().getShare_amount() + "（点击输入）");
        mBinding.etPrice3.setHint("当前余额" + data.getUser().getTeam_amount() + "（点击输入）");


    }

    @Override
    public void pay(String s, final String order) {
        ToastUtil.showShort("购买成功");
        finish();
    }
}
